package com.ribbon;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;


/**
 * Created by wangjing on 2018/7/26.
 *
 * 注解形式实现响应式的封装命令
 */
public class UserObservableService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand
    public Observable<User> getUserById(final String id){
        return Observable.create(new Observable.OnSubscribe<User>(){


            @Override
            public void call(Subscriber<? super User> observer) {
                if(!observer.isUnsubscribed()){
                    User user = restTemplate.getForObject("http",User.class,id);
                    observer.onNext(user);
                    observer.onCompleted();
                }
            }
        });
    }


}
